package com.parse.endpoint;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.cloud.GenerateWordCloud;
import com.commons.Constants;
import com.commons.ParsingResult;
import com.commons.ParsingService;
import com.parse.antlr.tree.parsers.service.ANTLRParsingService;
import com.parse.tree.parsers.service.java.JavaParsingService;

import dao.TagDAO;

public class ParserEndPoint {

	ApplicationContext appContext = new FileSystemXmlApplicationContext(
			"src/main/java/resources/applicationContext.xml");

	TagDAO dao = (TagDAO) appContext.getBean("tagDao");

	public void parse(String dirName, String parserType) {
		//By Default Java Parser is used
		ParsingService parsingService = new JavaParsingService();
		System.out.println("############## Parsing Starts: might take a while ##############");
		try {

			if (parserType.equalsIgnoreCase("g")) {
				// TODO 2: Parsing service for Antlr
				parsingService = new ANTLRParsingService();
			}

			Map<String, ParsingResult> parseDirectoryResult;
			parseDirectoryResult = parsingService.parseDirectory(dirName);

			System.out.println("############## Parsing Ends ##############");
			Set<Entry<String, ParsingResult>> entrySet = parseDirectoryResult
					.entrySet();
			//Logging
			for (Entry<String, ParsingResult> resultPerFile : entrySet) {
				System.out.println("\nResult for file name:"
						+ resultPerFile.getKey());
				System.out.println(resultPerFile.getValue() + "\n");
				System.out
						.println("#################################################");
			}

			for (Entry<String, ParsingResult> resultPerFile : entrySet) {

				ParsingResult result = resultPerFile.getValue();
				String fileName = resultPerFile.getKey();
				if(result.getKeywordList()!=null){
					for (String keyword : result.getKeywordList()) {
						dao.tagProcessing(keyword, Constants.Keyword_ID, fileName);
					}
				}
				if(result.getTypeNodes()!=null){
					for (String type : result.getTypeNodes()) {
						dao.tagProcessing(type, Constants.Datatype_ID, fileName);
					}
				}
				if(result.getVariableList()!=null){
					for (String var : result.getVariableList()) {
						dao.tagProcessing(var, Constants.Variable_ID, fileName);
					}
				}
				if(result.getStringNodes()!=null){
					for(String string : result.getStringNodes()){
						dao.tagProcessing(string, Constants.String_ID, fileName);
					}
				}
				if(result.getStrcharNodes()!=null){
					for(String strchar : result.getStrcharNodes()){
						dao.tagProcessing(strchar, Constants.Strchar_ID, fileName);
					}
				}
				else {
					System.out.println("Null result");
				}
			}

			// Call to word cloud
			GenerateWordCloud.invokeWordCloud(dao);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public static void main(String srgs[]) throws Throwable {
		ParserEndPoint p = new ParserEndPoint();
		p.parse("/Volumes/DevBox/workspace/a/penguinte/src/main/java/com/cloud", "java");
	}
}
